/***************************************************************

this file is part of Music Practice Audio Player
Copyright (C)2012 Serhii Ratsyborynskyi (ratz@live.ru)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

******************************************************************/

#include "MPAMixer.hpp"
#include <QDebug>

using namespace AudioCore;

MPAMixer::MPAMixer(QString path, QObject *parent):QIODevice(parent)
{
    QDir dirInfo;
    QStringList filter;
    filter << "*.wav";
    mClip_level = 0;
    QStringList files = dirInfo.entryList(filter);

    foreach (QString path, files)
    {
        IAudio * p = new QWavFile(path);
        if(p != 0)
        {
            pAudioSources.push_back(p);
        }
    }

    //pAudioSources.push_back(new QWavFile(files.front()));

    qDebug() << "MPA Mixer will mix " << pAudioSources.size() << " files";
    pAudioDataBuffer = new char[50000];
    mAudioFormat = pAudioSources.back()->getAudioFormat();
    mReady = true;
}

bool MPAMixer::open(QIODevice::OpenMode mode)
{
    bool result = true;
    foreach (IAudio * audioSource , pAudioSources)
    {
        result &= audioSource->open(mode);
        if(!result)
        {
              mErrorLog.append(audioSource->getState());
        }
    }
    result &=IAudio::QIODevice::open(mode);
    mReady = result;
    return result;
}

QAudioFormat MPAMixer::getAudioFormat()
{
    return mAudioFormat;
}

bool MPAMixer::isReady()
{
    return true;
}

QString MPAMixer::getState()
{
    return mErrorLog;
}

qint64 MPAMixer::readData(char *data, qint64 maxlen)
{
    qint64 resultlen = 0;
    qint64 len = 0;

    //pAudioDataBuffer = new char[static_cast<int>(maxlen)];
    for(int i = 0; i < pAudioSources.size(); i++)
    {

        qint32 * dst = (qint32 *) data;
        qint32 * source = (qint32 *) pAudioDataBuffer;

        resultlen = pAudioSources[i]->read(pAudioDataBuffer, maxlen);
        len = resultlen/sizeof(qint32);
        while(len--)
        {
            if(i==0)
            {
                *dst++ = *source++;
            }
            else
            {
                *dst++ += *source++;
            }

        }
    }
    //delete pAudioDataBuffer;
    return resultlen;
}

qint64 MPAMixer::writeData(const char *data, qint64 len)
{
    Q_UNUSED(( data ));
    Q_UNUSED(( len ));
    return 0;
}

const int MPAMixer::mBufferSize = 50000;
